package com.hzt.business.controller;

import com.hzt.business.domain.Provider;
import com.hzt.business.service.ProviderService;
import com.hzt.business.vo.ProviderVo;
import com.hzt.system.common.Constant;
import com.hzt.system.common.DataGridView;
import com.hzt.system.common.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
* 
*
* @Author:hzt
* @Date:6:52 下午 2020/7/25
*/
@RequestMapping("api/provider")
@RestController
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    @RequestMapping("loadAllProvider")
    public Object loadAllProvider(ProviderVo providerVo){
        return this.providerService.queryAllProvider(providerVo);
    }


    @RequestMapping("addProvider")
    public ResultObj addProvider(Provider provider){

        try {
            provider.setAvailable(Constant.AVAILABLE_TRUE);
            providerService.saveProvider(provider);
            return  ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return  ResultObj.ADD_SUCCESS;
        }
    }


    @RequestMapping("updateProvider")
    public ResultObj updateProvider(Provider provider){
        try {
            providerService.updateProvider(provider);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }


    @RequestMapping("deleteProvider")
    public ResultObj deleteProvider(Provider provider){
        try {
            providerService.removeById(provider.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    //批量删除
    @RequestMapping("batchDeleteProvider")
    public ResultObj batchDeleteProvider(Integer[] ids){
        try {
            List<Integer> listIds=new ArrayList<>();
            for (Integer id : ids) {
                listIds.add(id);
            }
            providerService.removeByIds(listIds);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }


    //下拉查询所有
    @RequestMapping("getAllAvailableProvider")
    public DataGridView getAllAvailableProvider(){
        return this.providerService.getAllAvailableProvider();
    }

}
